﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Security.Claims;

namespace Signalrs.HubModel
{
    [Authorize("xxxxx")]
    public class ChatRoomHub : Hub
    {

        public ImportExecutor importExecutor1;

        public ChatRoomHub(ImportExecutor importExecutor)
        {
            importExecutor1 = importExecutor;
        }

        /// <summary>
        /// 解析文件
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public Task SendProgress(string filenames)
        {
            var fromUser = Context.UserIdentifier;

            //await Clients.User(fromUser).SendAsync("ReceiveProgress", 0);
            _ = importExecutor1.ExcuteAsync(fromUser, filenames);
            return Task.CompletedTask;
        }

        //私发消息
        public async Task SendPriviteMessage(string toUserid, string message)
        {
            var fromUser = Context.UserIdentifier;
            if (string.IsNullOrEmpty(fromUser))
                return;
            //this.Clients.OthersInGroup("").SendAsync();
            await Clients.User(toUserid).SendAsync("ReceivePrivateMessage", fromUser, message);
        }

        //公共发送消息
        public Task SendPublicMessage(string message)
        {
            var userId = Context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            string connld = this.Context.ConnectionId;
            string msg = $"{connld} {DateTime.Now}:{message}";
            //this.Clients.Caller; // 自己
            //this.Clients.Group("dev"); 组
            //this.Clients.Others; //其他人
            //this.Clients.OthersInGroup(""); //组中其他人 不包含自己
            //this.Clients.User("");
            //this.Clients.Users();
            //this.Groups.AddToGroupAsync("", "");
            return Clients.All.SendAsync("ReceivePublicMessage", msg, "123");
        }
    }
}
